/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.base.bean;

import com.ibatis.dao.client.DaoManager;
import com.yunhoo.base.db.dao.AssetGBCodeDao;
import com.yunhoo.base.model.AssetGBCode;
import com.yunhoo.sys.db.config.DaoConfig;
import com.yunhoo.sys.util.tree.MyTreeNodeImpl;
import com.yunhoo.sys.util.tree.TreeModel;
import com.yunhoo.sys.util.tree.TreeRule;
import com.yunhoo.sys.util.tree.TreeUtil;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.richfaces.component.UITree;
import org.richfaces.event.TreeToggleEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;

@ManagedBean(name = "assetGBCodeBean")
@SessionScoped
public class AssetGBCodeBean {

    private static final DaoManager daom = DaoConfig.getDaoManager();
    private static final AssetGBCodeDao dao = (AssetGBCodeDao) daom.getDao(AssetGBCodeDao.class);
    private static final String[] rule = TreeRule.getAssetGBCodeTreeRule();
    //
    private AssetGBCode node = null;
    private TreeNode rootNode = null;
    private Set<AssetGBCode> gbCodeSet = null;
    //

    public static void main(String[] args) {
        AssetGBCode asset1 = new AssetGBCode();
        asset1.setCode("01");
        AssetGBCode asset2 = new AssetGBCode();
        asset2.setCode("02");
        AssetGBCode asset3 = new AssetGBCode();
        asset3.setCode("01");
        System.out.println(asset1.equals(asset2));
        System.out.println(asset1.equals(asset3));
    }

    public void assetToggleListener() {
        System.out.println("toggleListener !!");
        List<AssetGBCode> list = null;
        try {
            list = dao.queryList();
        } catch (SQLException ex) {
            Logger.getLogger(AssetGBCodeBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("list : " + list);
        if (list != null) {
            System.out.println("list : " + list.size());
        }
    }


    private void loadTree() {
        System.out.println("load cateTree");
        try {
            rootNode = new TreeNodeImpl();
            List<AssetGBCode> list = dao.queryAll();
            TreeUtil.addNode(list, rootNode, rule);
        } catch (SQLException ex) {
            Logger.getLogger(CategoryBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void loadTree_() {
        //延迟加载
        System.out.println("load cateTree");
        if (gbCodeSet == null) {
            gbCodeSet = new HashSet<AssetGBCode>();
        }
        try {
            rootNode = new TreeNodeImpl();
            List<AssetGBCode> list = dao.queryList();
            gbCodeSet.addAll(list);
            List<AssetGBCode> templi = Arrays.asList((AssetGBCode[]) gbCodeSet.toArray());
            TreeUtil.addNode(templi, rootNode, rule);
        } catch (SQLException ex) {
            Logger.getLogger(CategoryBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public TreeNode getRootNode() {
        if (this.rootNode == null) {
            loadTree();
        }
        return rootNode;
    }
}
